<?php

/**
 * Created by Reliese Model.
 */

namespace App\Models;

use Carbon\Carbon;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;

/**
 * Class Worker
 *
 * @property int $id
 * @property string|null $name
 * @property int|null $sex
 * @property string|null $birthday
 * @property string|null $phoneNum
 * @property string|null $contactNum
 * @property string|null $password
 * @property string|null $icon
 * @property Carbon|null $inTime
 * @property Carbon|null $positive_time
 * @property Carbon|null $outTime
 * @property string|null $id_type
 * @property string|null $id_number
 * @property string|null $remark
 * @property int|null $id_types
 * @property int|null $old_id
 * @property int|null $user_status
 * @property int|null $account_status
 * @property string|null $email
 * @property Carbon|null $create_time
 * @property Carbon|null $update_time
 * @property int|null $is_positive
 *
 * @property Collection|ExcelFile[] $excel_files
 * @property Collection|Image[] $images
 * @property Collection|MessageRecord[] $message_records
 * @property Collection|OaFlow[] $oa_flows
 * @property Collection|OaRecord[] $oa_records
 * @property Collection|Order[] $orders
 * @property Collection|OrderRecord[] $order_records
 * @property Collection|RecordWorkerLogin[] $record_worker_logins
 * @property Collection|SystemOrder[] $system_orders
 * @property Collection|SystemOrderRecord[] $system_order_records
 * @property Collection|SystemRecord[] $system_records
 * @property Collection|TbDocument[] $tb_documents
 * @property Collection|User[] $users
 * @property Collection|Branch[] $branches
 * @property Collection|Role[] $roles
 *
 * @package App\Models
 */
class Worker extends Model
{
	protected $table = 'worker';

	protected $casts = [
		'sex' => 'int',
		'id_types' => 'int',
		'old_id' => 'int',
		'user_status' => 'int',
		'account_status' => 'int',
		'is_positive' => 'int'
	];

//	protected $dates = [
//		'inTime',
//		'positive_time',
//		'outTime'
//	];

	protected $hidden = [
		'password'
	];

	protected $fillable = [
		'name',
		'sex',
		'birthday',
		'phoneNum',
		'contactNum',
		'password',
		'icon',
		'inTime',
		'positive_time',
		'outTime',
		'id_type',
		'id_number',
		'remark',
		'id_types',
		'old_id',
		'user_status',
		'account_status',
		'email',
		'is_positive'
	];

    public function history_data()
    {
        return $this->hasMany(HistoryDatum::class);
    }

    public function images()
    {
        return $this->hasMany(Image::class, 'worker_id');
    }

    public function reocrd_images()
    {
        return $this->hasMany(Image::class, 'record_worker_id');
    }

    public function message_records()
    {
        return $this->hasMany(MessageRecord::class);
    }

    public function oa_flows()
    {
        return $this->hasMany(OaFlow::class);
    }

    public function submit_oa_records()
    {
        return $this->hasMany(OaRecord::class, 'submit_id');
    }

    public function oa_records()
    {
        return $this->hasMany(OaRecord::class, 'worker_id');
    }

    public function users()
    {
        return $this->hasMany(User::class, 'worker_id');
    }

    public function orders()
    {
        return $this->hasMany(Order::class, 'record_worker_id');
    }

    public function order_records()
    {
        return $this->hasMany(OrderRecord::class);
    }

    public function system_orders()
    {
        return $this->hasMany(SystemOrder::class);
    }

    public function system_order_record()
    {
        return $this->hasOne(SystemOrderRecord::class);
    }

    public function system_records()
    {
        return $this->hasMany(SystemRecord::class, 'record_worker_id');
    }


    public function history_users()
    {
        return $this->belongsToMany(User::class)
            ->withPivot('id', 'old_worker_id', 'state', 'remark')
            ->withTimestamps();
    }

    public function branches()
    {
        return $this->belongsToMany(Branch::class, 'worker_branch')
            ->withPivot('id')
            ->withTimestamps();
    }
    public function branchesManage()
    {
        return $this->belongsToMany(Branch::class, 'worker_branch_manage')
            ->withPivot('id')
            ->withTimestamps();
    }

    public function roles()
    {
        return $this->belongsToMany(Role::class, 'worker_role')
            ->withPivot('id')
            ->withTimestamps();
    }
}
